#include "log_sum_exp_tiling.h"
#include "register/op_def_registry.h"
#include "tiling/platform/platform_ascendc.h"

namespace optiling
{
    const uint32_t BLOCK_SIZE = 32;
    const uint32_t BUFFER_NUM = 2;
    static ge::graphStatus TilingFunc(gert::TilingContext *context)
    {

        LogSumExpTilingData tiling;

        const gert::TypedContinuousVector<int64_t> *dims = context->GetAttrs()->GetListInt(0);
        uint32_t dimsNum = dims->GetSize();

        int32_t y_ndarray[20], x_ndarray[20];
        int32_t y_dimensional, x_dimensional;

        // 获取原始shape
        auto shape_x = context->GetInputTensor(0)->GetOriginShape();
        auto shape_y = context->GetOutputShape(0)->GetOriginShape();

        x_dimensional = shape_x.GetDimNum();
        // y_dimensional = shape_y.GetDimNum();

        // 将维度较小的张量的高位填充为1, 如原shape = [2, 4], 则填充后为 [4, 2, 1], 逆序的
        for (int i = 0; i < x_dimensional; ++i)
        {
            x_ndarray[x_dimensional - i - 1] = shape_x.GetDim(i);
            y_ndarray[x_dimensional - i - 1] = shape_x.GetDim(i);
        }
        for (int i = 0; i < dimsNum; ++i)
        {
            int32_t dim = *(dims->GetData() + i);
            if (dim < 0)
            {
                dim += x_dimensional;
            }
            y_ndarray[x_dimensional - dim - 1] = 1;
        }
        tiling.set_x_dimensional(x_dimensional);
        // tiling.set_y_dimensional(y_dimensional);
        tiling.set_y_ndarray(y_ndarray);
        tiling.set_x_ndarray(x_ndarray);

        // 每个张量的累积形状数组，如 y_ndarray 为 [4, 2, 2]，则 y_sumndarray 为 [1, 4, 8, 16]
        int32_t y_sumndarray[20], x_sumndarray[20];
        y_sumndarray[0] = 1;
        x_sumndarray[0] = 1;
        for (int i = 1; i <= x_dimensional; ++i)
        {
            y_sumndarray[i] = y_sumndarray[i - 1] * y_ndarray[i - 1];
            x_sumndarray[i] = x_sumndarray[i - 1] * x_ndarray[i - 1];
        }
        tiling.set_y_sumndarray(y_sumndarray);
        tiling.set_x_sumndarray(x_sumndarray);

        context->SetBlockDim(1);
        tiling.SaveToBuffer(context->GetRawTilingData()->GetData(), context->GetRawTilingData()->GetCapacity());
        context->GetRawTilingData()->SetDataSize(tiling.GetDataSize());

        size_t *currentWorkspace = context->GetWorkspaceSizes(1);
        currentWorkspace[0] = 0;

        return ge::GRAPH_SUCCESS;
    }
}

namespace ge
{
    static ge::graphStatus InferShape(gert::InferShapeContext *context)
    {
        const gert::Shape *x1_shape = context->GetInputShape(0);
        gert::Shape *y_shape = context->GetOutputShape(0);
        *y_shape = *x1_shape;
        return GRAPH_SUCCESS;
    }
}

namespace ops
{
    class LogSumExp : public OpDef
    {
    public:
        explicit LogSumExp(const char *name) : OpDef(name)
        {
            this->Input("x")
                .ParamType(REQUIRED)
                .DataType({ge::DT_FLOAT, ge::DT_FLOAT16})
                .Format({ge::FORMAT_ND, ge::FORMAT_ND})
                .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND});
            this->Output("y")
                .ParamType(REQUIRED)
                .DataType({ge::DT_FLOAT, ge::DT_FLOAT16})
                .Format({ge::FORMAT_ND, ge::FORMAT_ND})
                .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND});
            this->Attr("dim").AttrType(OPTIONAL).ListInt({0});
            this->Attr("keep_dim").AttrType(OPTIONAL).Bool(false);

            this->SetInferShape(ge::InferShape);

            this->AICore()
                .SetTiling(optiling::TilingFunc);
            this->AICore().AddConfig("ascend910b").AddConfig("ascend310b");
        }
    };

    OP_ADD(LogSumExp);
}
